package Scuttlebutt;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.*;
import java.awt.GridLayout;

public class OptionsDlg extends JDialog 
{
	Scuttlebutt parent = null;
	boolean state = false;
	JTextField ftpAddress = null;
	JTextField port = null;
	JTextField username = null;
	JPasswordField password = null;	

	OkButtonAction OkAction = null;
	CancelButtonAction cancelAction = null;
	ApplyButtonAction applyAction = null;
	
	JButton OK = null;
	JButton cancel = null;
	JButton apply = null;
	
	public class ApplyButtonAction extends AbstractAction {
		OptionsDlg dlg = null;
		
        public ApplyButtonAction(String text, Integer mnemonic, OptionsDlg dlg) {
            super(text, null);
            putValue(MNEMONIC_KEY, mnemonic);
            this.dlg = dlg;
        }
        public void actionPerformed(ActionEvent e) {
	        dlg.update();
	    }
    }	
	
	public class OkButtonAction extends AbstractAction {
		OptionsDlg dlg = null;
       	public OkButtonAction(String text, Integer mnemonic, OptionsDlg dlg) {
            super(text, null);
            putValue(MNEMONIC_KEY, mnemonic);
            this.dlg = dlg;
        }
        public void actionPerformed(ActionEvent e) {
	        dlg.update();
	        setVisible(false);
        }
    }	
	
	
	public class CancelButtonAction extends AbstractAction {
		OptionsDlg dlg = null;
        public CancelButtonAction(String text, Integer mnemonic, OptionsDlg dlg) {
            super(text, null);
            putValue(MNEMONIC_KEY, mnemonic);
            this.dlg = dlg;
        }
        public void actionPerformed(ActionEvent e) {
	        setVisible(false);
        }
    }	
	
/*	public class OptionsPanels extends JSplitPane{
		public OptionsPanels(){
			setOrientation(JSplitPane.HORIZONTAL_SPLIT);
			setLeftComponent(new JScrollPane(new JTextArea("Left")));
			setRightComponent(new JScrollPane(new JTextArea("Right")));
//			setDividerLocation(parent.getSettings().getLowerDivider());
			
		}
	}
*/	
	
	public OptionsDlg(Scuttlebutt parent, String title, boolean modal)
	{
		super(parent, title, modal);
		this.parent = parent;
		init();	
		setLocationRelativeTo(parent);
		
		boolean enable = (parent.isConnected())?false:true;
		ftpAddress.setEnabled(enable);
		port.setEnabled(enable);
		username.setEnabled(enable);
		password.setEnabled(enable);
		OK.setEnabled(enable);
		apply.setEnabled(enable);
	}
	
	private void init(){
		setSize(new Dimension(400,300));
		Container container=getContentPane();
		container.setLayout(null);
		setupDialog(container);
	}
	
	public void setupDialog(Container pane){
		OkAction = new OkButtonAction("OK",new Integer(KeyEvent.VK_O), this);
		applyAction = new ApplyButtonAction("Apply",new Integer(KeyEvent.VK_A), this);
		cancelAction = new CancelButtonAction("Cancel",new Integer(KeyEvent.VK_C), this);
		
		Insets insets = pane.getInsets();
		int headspace = 5;
		int between = 5;
		int left_margin = 20;
		
		int margin = (int)(getSize().getWidth()/4);
		
		int y = insets.top + headspace;
		
		JLabel l = new JLabel("Project Name:");
		pane.add(l);
		Dimension size = l.getPreferredSize();
		l.setBounds(margin - between -  size.width, y, size.width, size.height);
		
		JLabel projectTitle = new JLabel(parent.getProject().getProjectTitle());
		pane.add(projectTitle);
		size = projectTitle.getPreferredSize();
		projectTitle.setBounds(margin, y, size.width, size.height);
		
		y += size.height + headspace;
		
		JLabel a = new JLabel("Address:");
		pane.add(a);
		size = a.getPreferredSize();
		a.setBounds(margin - between -  size.width, y, size.width, size.height);
		
		ftpAddress = new JTextField(parent.getProject().getAddress(),20);
		pane.add(ftpAddress);
		size = ftpAddress.getPreferredSize();
		ftpAddress.setBounds(margin, y, size.width, size.height);
		
		y += size.height + headspace;
		
		JLabel p = new JLabel("Port:");
		pane.add(p);
		size = p.getPreferredSize();
		p.setBounds(margin - between -  size.width, y, size.width, size.height);
		
		port = new JTextField(new Integer(parent.getProject().getPort()).toString(),3);
		pane.add(port);
		size = port.getPreferredSize();
		port.setBounds(margin, y, size.width, size.height);
		
		y += size.height + headspace;
		
		JLabel u = new JLabel("Username:");
		pane.add(u);
		size = u.getPreferredSize();
		u.setBounds(margin - between -  size.width, y, size.width, size.height);
		
		username = new JTextField(parent.getProject().getUsername(),15);
		pane.add(username);
		size = username.getPreferredSize();
		username.setBounds(margin, y, size.width, size.height);
		
		y += size.height + headspace;
		
		JLabel pw = new JLabel("Password:");
		pane.add(pw);
		size = pw.getPreferredSize();
		pw.setBounds(margin - between -  size.width, y, size.width, size.height);
		
		password = new JPasswordField(parent.getProject().getPassword(),15);
		pane.add(password);
		size = password.getPreferredSize();
		password.setBounds(margin, y, size.width, size.height);

		y += size.height + headspace;

		apply = new JButton(applyAction);
		pane.add(apply);
		size = apply.getPreferredSize();
		int width = 75;
		int x = getWidth() - width - 20 - insets.right;
		System.out.println("width:" + getWidth());
		System.out.println("x:" + x);
		apply.setBounds(x, y, width, size.height);
		
		cancel = new JButton(cancelAction);
		pane.add(cancel);
		size = cancel.getPreferredSize();
		x -= width + 2;
		System.out.println("x:" + x);
		cancel.setBounds(x, y, width, size.height);
		
		OK = new JButton(OkAction);
		pane.add(OK);
		size = OK.getPreferredSize();
		x -= width + 2;
		System.out.println("x:" + x);
		OK.setBounds(x, y, width, size.height);
		System.out.println("y:" + y);
		y += OK.getHeight() + headspace;
		System.out.println("y:" + y);
		
		setSize(new Dimension(400,185));
		
		
	}
	
	public boolean getState(){
		return state;
	}
	
	public void update(){
       if(ftpAddress.getText().length() > 0){
  	      parent.getProject().ftpAddress = ftpAddress.getText();
        }else{
	        JOptionPane.showMessageDialog(null, "FTP address cannot be blank", "Error", JOptionPane.ERROR_MESSAGE);
	        ftpAddress.grabFocus();
	        return;
        }
        if(port.getText().length() > 0){
  	      parent.getProject().port = new Integer(port.getText()).intValue();
        }else{
	        JOptionPane.showMessageDialog(null, "Port cannot be blank", "Error", JOptionPane.ERROR_MESSAGE);
	        port.grabFocus();
	        return;
        }
        if(username.getText().length() > 0){
  	      parent.getProject().username = username.getText();
        }else{
	        JOptionPane.showMessageDialog(null, "FTP address cannot be blank", "Error", JOptionPane.ERROR_MESSAGE);
	        username.grabFocus();
	        return;
        }
        if(password.getText().length() > 0){
  	      parent.getProject().password = password.getText();
        }else{
	        JOptionPane.showMessageDialog(null, "FTP address cannot be blank", "Error", JOptionPane.ERROR_MESSAGE);
	        password.grabFocus();
	        return;
        }
	}
}

